//
//  UIButton+Blocks.m
//  DecoAware
//
//  Created by APP on 16/7/6.
//  Copyright © 2016年 Sunyoung. All rights reserved.
//

#import "UIButton+Blocks.h"
#import <objc/runtime.h>

@implementation UIButton (Blocks)

static char overviewKey = 'b';

-(void) addButtonBlockControlEvent:(UIControlEvents)event withBlock:(buttonEventBlockHandle)buttonEventTouchUpInSideBlockHandle
{
    objc_removeAssociatedObjects(self);
    objc_setAssociatedObject(self, &overviewKey, buttonEventTouchUpInSideBlockHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
    //objc_setAssociatedObject(self, &overviewKey, buttonEventTouchUpInSideBlockHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];
}

- (void)callActionBlock:(id)sender
{
    buttonEventBlockHandle block = (buttonEventBlockHandle)objc_getAssociatedObject(self, &overviewKey);
    if (block)
    {
        block(sender);
    }
}

- (void)dealloc
{
    buttonEventBlockHandle block = (buttonEventBlockHandle)objc_getAssociatedObject(self, &overviewKey);
    if(block)
        block = nil;
    objc_removeAssociatedObjects(self);
}
@end
